
var orderApp = angular.module("orderApp",[]);

orderApp.controller("orderCtrl",function ($scope,$http,$rootScope) {
    //共享数据


});

orderApp.controller("orderListCtrl",function ($scope,$rootScope,$http) {
    $scope.showOrderDetail = function (index) {
        $scope.ulList[index] = ! $scope.ulList[index*1];
    }

    $scope.showScoreDiv = function (index) {
        $rootScope.isShowScore = true;
        $rootScope.scoreWhich = index;
    }

    $scope.showComplaintDiv = function (index) {
        $rootScope.isShowComplaint = true;
        $rootScope.complaintWhich = index;
    }

    $scope.getComplaintResult =function (index) {
        var req = {
            url:"/CHa/complaint?oid="+$scope.orders[index]._id,
            method:"GET"
        }
        $http(req).then(function (res) {
            $rootScope.ComplaintResult = res.data.result;
        })
    }
    
    $scope.receiveGoods = function (index) {
        var req = {
            url:"/CHa/order/"+$scope.orders[index]._id,
            method:"PUT",
            data:{taken:-1},
            headers:{
                "Content-type":"application/json"
            }
        }
        $http(req).then(function (res) {
            console.log(res.data);
            $scope.orders[index].taken = res.data.taken;
        })
    }

});

orderApp.controller("complaintCtrl",function ($scope,$http,$rootScope) {
    $scope.complaintContent = "";

    $scope.exitComplaint = function () {
        $rootScope.isShowComplaint = false;
    }

    $scope.submitComplaint = function () {
        var orderComplaintReq = {
            url:"/CHa/order/"+$scope.orders[$rootScope.complaintWhich]._id,
            method:"PUT",
            data:{comments:$scope.complaintContent},
            headers:{
                "Content-type":"application/json"
            }
        };

        $http(orderComplaintReq).then(function(res){
            $rootScope.isShowComplaint = false;
            $scope.orders[$rootScope.complaintWhich] = res.data;
        },function (res) {
            alert("评分失败")
        });
    }
});
orderApp.controller("scoreCtrl",function ($scope,$http,$rootScope) {
    $scope.fullStar = "images/fullStar.gif";
    $scope.emptyStar = "images/emptyStar.gif";
    $scope.starList = [];
    for(var i = 0; i < 10; i++ ){
        $scope.starList.push(i);
    }

    $scope.fullStarLength = 0;
    $scope.scoreStar = function (index) {
        $scope.fullStarLength = index+1;
    }

    $scope.exitScore = function () {
        $rootScope.isShowScore = false;
        $scope.fullStarLength = 0;
    };

    $scope.submitScore = function () {
        var orderScoreReq = {
            url:"/CHa/order/"+$scope.orders[$rootScope.scoreWhich]._id,
            method:"PUT",
            data:{score:$scope.fullStarLength},
            headers:{
                "Content-type":"application/json"
            }
        };
        $http(orderScoreReq).then(function(res){
            $rootScope.isShowScore = false;
            $scope.fullStarLength = 0;
            $scope.orders[$rootScope.scoreWhich] = res.data;
        },function (res) {
            alert("评分失败");
        })
    };
});

orderApp.controller('userCtrl',function ($scope,$http,$rootScope) {
    var req = {
        method:"GET",
        url:"/CHa/session"
    }

    $http(req)
        .then(function (res) {
            if(res.data.account!=undefined){
                $rootScope.uid = res.data.uid;
                $scope.userAccount = res.data.account;
                $scope.hasLogin = true;
                //共享数据
                $rootScope.ComplaintResult = "未处理";
                var getOrdersReq = {
                    url:"/CHa/orders?uid="+$rootScope.uid,
                    method:"GET"
                }
                $http(getOrdersReq).then(function (res) {

                    $scope.orders = res.data;
                    for(var i = 0; i < $scope.orders.length; i++){
                        var shanghai    = moment.tz($scope.orders[i].orderTime, "Asia/Shanghai");
                        $scope.orders[i].orderTime = shanghai.format('MMMM Do YYYY, h:mm a');
                    }
                    $scope.ulList = [];
                    var ulListLength = $scope.orders.length;
                    for(var i = 0; i < ulListLength; i++){
                        $scope.ulList.push(false);
                    }
                })
                $rootScope.isShowScore = false;

                $rootScope.scoreWhich = 0;

                $rootScope.isShowComplaint = false;

                $rootScope.complaintWhich = 0;
            }else{
                $scope.hasLogin = false;
            }


        });

    $scope.logOut = function () {
        var clearSeesionReq = {
            url:"/CHa/session",
            method:"PUT"
        }
        $http(clearSeesionReq).then(function (res) {
            window.location.href = "home.html";
        })

    }
});
